﻿using Microsoft.Practices.Prism.StoreApps;
using Microsoft.Practices.Prism.StoreApps.Interfaces;
using Newtonsoft.Json;
using Sumc.Client.ModernUI.Common;
using Sumc.Client.ModernUI.Extensions;
using Sumc.Client.ModernUI.Helpers;
using Sumc.Client.ModernUI.Services;
using Sumc.Models.Responses;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Threading.Tasks;
using Windows.UI.Xaml.Navigation;

namespace Sumc.Client.ModernUI.ViewModels
{
    public class HomePageViewModel : BaseViewModel
    {
        private DelegateCommand<string> goToStop;
        private DelegateCommand<NewsGlanceInformation> goToNews;
        private ObservableCollection<MainStop> mainStops;
        private ObservableCollection<NewsGlanceInformation> news;
        private readonly IStopRepository stopRepository;
        private readonly INewsRepository newsRepository;

        public HomePageViewModel(INavigationService navigationService,
            IAlertMessageService alertMessageService,
            IStopRepository stopRepository,
            INewsRepository newsRepository,
            ISearchPaneService searchPaneService,
            IFlyoutService flyoutService)
            : base(navigationService,
            alertMessageService,
            flyoutService,
            searchPaneService)
        {
            this.stopRepository = stopRepository;
            this.newsRepository = newsRepository;
        }

        public DelegateCommand<NewsGlanceInformation> GoToNews
        {
            get
            {
                if (this.goToNews == null)
                {
                    this.goToNews = new DelegateCommand<NewsGlanceInformation>(this.NavigateToNews);
                }

                return this.goToNews;
            }
        }

        public DelegateCommand<string> GoToStop
        {
            get
            {
                if (this.goToStop == null)
                {
                    this.goToStop = new DelegateCommand<string>(this.NavigateToStop);
                }

                return this.goToStop;
            }
        }

        public ObservableCollection<NewsGlanceInformation> News
        {
            get
            {
                return this.news;
            }

            set
            {
                this.SetProperty(ref this.news, value);
            }
        }

        public ObservableCollection<MainStop> MainStops
        {
            get
            {
                return this.mainStops;
            }

            set
            {
                this.SetProperty(ref this.mainStops, value);
            }
        }

        private void NavigateToStop(string stop)
        {
            this.navigationService.Navigate(Constants.StopPage, stop);
        }

        private async void NavigateToNews(NewsGlanceInformation newsGlanceInformation)
        {
            var jsonNews = await JsonConvert.SerializeObjectAsync(newsGlanceInformation);
            this.flyoutService.ShowFlyout(Constants.NewsFlyout, jsonNews, null);
        }

        private async Task GetNews()
        {
            var news = await this.newsRepository.GetNews();
            if (news != null)
            {
                this.News = new ObservableCollection<NewsGlanceInformation>();
                this.News.FillCollection(news);
            }
        }

        private async Task GetMainStops()
        {
            var stops = await this.stopRepository.GetMainStops();
            if (stops != null)
            {
                this.MainStops = new ObservableCollection<MainStop>();
                this.MainStops.FillCollection(stops);
            }
        }

        private void LoadData()
        {
            this.LoadData(async () =>
            {
                await this.GetNews();
                await this.GetMainStops();
            });
        }

        public override void OnNavigatedTo(object navigationParameter, NavigationMode navigationMode, Dictionary<string, object> viewModelState)
        {
            base.OnNavigatedTo(navigationParameter, navigationMode, viewModelState);
            this.LoadData();
        }
    }
}
